登录 白背景

https://leetcode-cn.com/problems/merge-two-sorted-lists/

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        if l1:
            i = l1.val
        if l2:
            i = l2.val
        if not l1 and not l2:
            i = 0
        if l1 and l2:
            i = l1.val if l1.val <= l2.val else l2.val
        newNode = ListNode(i)
        ret = newNode
        while True:
            while l1 and l1.val == i:
                ret.next = l1
                ret = ret.next
                l1 = l1.next
            while l2 and l2.val == i:
                ret.next = l2
                ret = ret.next
                l2 = l2.next
            if not l1 and not l2:
                break
            i += 1

        return newNode.next